<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="型号名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入配置名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="appId" prop="appId" label-width="80px">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入appId"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="scene" prop="scene" label-width="80px">
        <el-input
          v-model="queryParams.scene"
          placeholder="请输入scene"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aiui:param:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aiuiConfigList">
      <el-table-column label="配置名称" align="center" prop="name" fixed />
      <el-table-column label="appId" align="center" prop="appId" width="240">
        <template slot-scope="scope">
          <i class="el-icon-document-copy" v-clipboard:copy="scope.row.appId" v-clipboard:success="copyText" style="margin-right: 5px;cursor: pointer;"></i>
          {{ scope.row.appId }}
        </template>
      </el-table-column>
      <el-table-column label="情景模式" align="center" prop="scene" />
      <el-table-column label="secret" align="center" prop="secret" width="250" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <i class="el-icon-document-copy" v-clipboard:copy="scope.row.secret" v-clipboard:success="copyText" style="margin-right: 5px;cursor: pointer;"></i>
          {{ scope.row.secret }}
        </template>
      </el-table-column>
      <el-table-column label="appKey" align="center" prop="appKey" width="250" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <i class="el-icon-document-copy" v-clipboard:copy="scope.row.appKey" v-clipboard:success="copyText" style="margin-right: 5px;cursor: pointer;"></i>
          {{ scope.row.appKey }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aiui:param:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aiui:param:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              @click="updateConfigStatus(scope.row)"
              v-hasPermi="['aiui:param:query']"
          >
            <span v-show="parseInt(scope.row.status) == 1">停用</span>
            <span v-if="parseInt(scope.row.status) == 0">启用</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="应用名称:" prop="name" label-width="110px">
          <el-input v-model="form.name" placeholder="请输入配置名称"/>
        </el-form-item>
        <el-form-item label="appId:" prop="appId" label-width="110px">
          <el-input v-model="form.appId" placeholder="请输入appId"/>
        </el-form-item>
        <el-form-item label="情景模式:" prop="asrParamId" label-width="110px">
          <el-input v-model="form.asrParamId" placeholder="请输入asrParamId"/>
        </el-form-item>
        <el-form-item label="scene:" prop="scene" label-width="110px">
          <el-input v-model="form.scene" placeholder="请选择scene"/>
        </el-form-item>
        <el-form-item label="appKey:" prop="appKey" label-width="110px">
          <el-input v-model="form.appKey" placeholder="请输入appKey"/>
        </el-form-item>
        <el-form-item label="secret:" prop="secret" label-width="110px">
          <el-input v-model="form.secret" placeholder="请输入secret"/>
        </el-form-item>
        <el-form-item label="是否启用:" label-width="110px" >
          <el-radio-group v-model="form.status">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAiUiConfig, updateAiUiConfigStatus, insertAiUiConfig, deleteAiUiConfigList, selectAiUiConfig, updateAiUiConfig} from "@/api/platform/aiui/config";

export default {
  name: "AiUiConfig",
  data() {
    return {
      operationType: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      aiuiConfigList: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示添加修改弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        appId: [
          { required: true, message: "appId不能为空", trigger: "blur" }
        ],
        asrParamId: [
          { required: true, message: "asrParamId参数不能为空", trigger: "blur" }
        ],
        scene: [
          { required: true, message: "scene场景不能为空", trigger: "blur" }
        ],
        appKey: [
          { required: true, message: "appKey参数不能为空", trigger: "blur" }
        ],
        secret: [
          { required: true, message: "secret参数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listAiUiConfig(this.queryParams).then(response => {
        this.aiuiConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    statusFormat(row, index){
      if (row.status === 0) {
        return "未激活";
      } else if(row.status === 1){
        return "激活";
      }
    },
    //修改配置状态
    updateConfigStatus(row){
      let message = ""
      let data = {};
      data.status = row.status;
      data.uid = row.uid
      if(data.status == 0){
        message = "<div>确定要启用应用: <span>&nbsp;&nbsp;&nbsp;" + row.name + " ?</span></div>"+
          "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      }else{
        message = "<div>确定要停用应用: <span>" + row.name + " ?</span></div>"+
          "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      }
      this.$modal.confirm(message).then(function() {
        return updateAiUiConfigStatus(data);
      }).then(() => {
        if (row.status == 0) {
          this.$modal.msgSuccess("已启用");
        } else {
          this.$modal.msgSuccess("已停用");
        }
        this.getList();
      }).catch(() => {});
    },
    copyText(){
      this.$message.success("复制成功")
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加AIUI配置";
      let obj = {status : 0};
      this.form = obj;

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改配置";
      selectAiUiConfig(row.uid).then(response =>{
        this.form = response.data;
      })

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid == undefined) {
            let data = this.form;
            //提示
            const message = "<div>确定要新增配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return insertAiUiConfig(data);
            }).then(() => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          }else{
            let data = this.form;
            //提示
            const message = "<div>确定要修改配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return updateAiUiConfig(data);
            }).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {
        uidList: []
      };
      data.uidList.push(row.uid)
      //提示
      const message = "<div>确定要删除应用: <span>" + row.name + " ?</span></div>"+
        "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      this.$modal.confirm(message).then(function() {
        return deleteAiUiConfigList(data);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList()
      })
    },
  }
};
</script>
